//
//  SKAppDelegate.m
//  Sudoku
//
//  Created by Richard Hodgkins on 08/09/2012.
//  Copyright (c) 2012 Richard Hodgkins. All rights reserved.
//

#import "SKAppDelegate.h"

#import "SKCoreData.h"

#import "SKHomeViewController.h"
#import "SKNavigationController.h"

#define USER_DEFAULT_KEY_FIRST_LAUNCH @"firstLaunch"

@interface SKAppDelegate ()

@property (nonatomic, strong) UINavigationController* navController;

@end

@implementation SKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    DLog(@"finshedLaunching");
        
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults objectForKey:USER_DEFAULT_KEY_FIRST_LAUNCH]) {
        [defaults setObject:[NSDate date] forKey:USER_DEFAULT_KEY_FIRST_LAUNCH];
        
        DLog(@"Adding data");
        [SKCoreData addInitialData];
        DLog(@"Finished adding data");
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    SKHomeViewController *rootController = [[SKHomeViewController alloc] init];
    
    self.navController = [[UINavigationController alloc] initWithRootViewController:rootController];
    
    self.window.rootViewController = self.navController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    DLog(@"APP RESIGNED ACTIVE");
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    DLog(@"APP ENTERED BACKGROUND");
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [[SKCoreData sharedInstance] saveContext];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    DLog(@"APP ENTERED FOREGOUND");
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    DLog(@"APP BECAME ACTIVE");
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    DLog(@"APP TERMINATED");
    // Saves changes in the application's managed object context before the application terminates.
    [[SKCoreData sharedInstance] saveContext];
}

- (void) applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    DLog(@"APP RECEIVED MEMORY WARNING");
}

@end
